{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index =0\n"
     ]
    }
   ],
   "source": [
    "fevent = open(\"events.csv\")\n",
    "snum = 15\n",
    "iindex=0\n",
    "uniqueEvents = set()\n",
    "fevent_train = open(\"events_train_test.csv\",\"wb\")\n",
    "\n",
    "\n",
    "#读取第一行标签行，并写入events_train_test.csv文件中\n",
    "slabel = fevent.readline()\n",
    "fevent_train.write(slabel)\n",
    "\n",
    "#读出train.csv和test.csv两个文件的eventid\n",
    "for filename in [\"train.csv\", \"test.csv\"]:\n",
    "    f = open(filename, 'rb')\n",
    "    \n",
    "    #忽略第一行（列名字）\n",
    "    f.readline().strip().split(\",\")\n",
    "    \n",
    "    for line in f:    #对每条记录\n",
    "        cols = line.strip().split(\",\")\n",
    "        uniqueEvents.add(cols[1])   #第二列为活动ID\n",
    "    f.close()\n",
    "\n",
    "\n",
    "for line in fevent:\n",
    "    if(line != \"\"):\n",
    "        cols = line.strip().split(\",\")\n",
    "        eventid = cols[0]\n",
    "        if(eventid in uniqueEvents):\n",
    "            fevent_train.write(line)\n",
    "        \n",
    "fevent.close()\n",
    "fevent_train.close()\n",
    "print(\"数据整理完毕。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
